import apis from "../services/AddSceneApi";

export default {
  namespace: "addScene",
  state: {
    addErrCode: "-1",
    modErrCode: "-1",
    detailScene: {},
    loading: false,
    paramList: [],
    errMsg: "",
    key: {},
    keyDetail: {}
  },
  subscriptions: {
    // 监听地址工作台
    setup({ dispatch, history }) {
      history.listen(location => {
        if (
          location.pathname ===
            "/intellPush/recommendCenter/situation/addScene" ||
          location.pathname ===
            "/intellPush/recommendCenter/situation/editScene"
        ) {
          if (location.search.indexOf("key") >= 0) {
            const key = JSON.parse(
              window.decodeURIComponent(location.search).substring(5)
            );
            dispatch({ type: "put/putkey", payload: key });
            // if (key.operate === "mod") {
            //   dispatch({
            //     type: "detailScene",
            //     payload: { sceneTemplateId: key.sceneTemplateId }
            //   });
            // }
          }
        }
        if (
          location.pathname ===
          "/intellPush/recommendCenter/situation/sceneDetail"
        ) {
          if (location.search.indexOf("key") >= 0) {
            const key = JSON.parse(
              window.decodeURIComponent(location.search).substring(5)
            );
            dispatch({ type: "put/putkeyDetail", payload: key });
            // if (key.operate === "mod") {
            //   dispatch({
            //     type: "detailScene",
            //     payload: { sceneTemplateId: key.sceneTemplateId }
            //   });
            // }
          }
        }
      });
    }
  },
  effects: {
    // 推送数量趋势图
    *AddScene({ payload }, { call, put }) {
      const res = yield call(apis.AddScene, payload);
      const { errCode = "-1", errMsg } = res;
      yield put({
        type: "put/addError",
        payload: errCode
      });
      yield put({
        type: "put/errMsg",
        payload: errMsg
      });
    },
    *ModScene({ payload }, { call, put }) {
      const res = yield call(apis.ModScene, payload);
      const { errCode = "-1", errMsg } = res;
      yield put({
        type: "put/modError",
        payload: errCode
      });
      yield put({
        type: "put/errMsg",
        payload: errMsg
      });
    },
    *detailScene({ payload }, { call, put }) {
      yield put({
        type: "put/loading",
        payload: true
      });
      const res = yield call(apis.detailScene, payload);
      const { datas = {} } = res;
      yield put({
        type: "put/detail",
        payload: datas
      });
      yield put({
        type: "put/loading",
        payload: false
      });
    },
    *getParams({ payload }, { call, put }) {
      const res = yield call(apis.getParams, payload);
      const { datas = [] } = res;
      yield put({
        type: "put/putparam",
        payload: datas === null ? [] : datas
      });
    }
  },
  reducers: {
    "put/addError": (state, { payload }) => ({
      ...state,
      addErrCode: payload
    }),
    "put/modError": (state, { payload }) => ({
      ...state,
      modErrCode: payload
    }),
    "put/detail": (state, { payload }) => ({
      ...state,
      detailScene: payload
    }),
    "put/putkey": (state, { payload }) => ({
      ...state,
      key: payload
    }),
    "put/loading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/putparam": (state, { payload }) => ({
      ...state,
      paramList: payload
    }),
    "put/errMsg": (state, { payload }) => ({
      ...state,
      errMsg: payload
    }),
    "put/putkeyDetail": (state, { payload }) => ({
      ...state,
      keyDetail: payload
    })
  }
};
